#include "signature_manager.h"

#include <mutex>
#include <thread>

#ifdef DEBUG_PRINTS_LEVEL_1
#include <iostream>
#include <iomanip>
#endif






PlainMessage::~PlainMessage() {
    if (hash_function != nullptr){
        delete hash_function;
    }
    hash_function = nullptr;
}

HASH_VALUE PlainMessage::get_hash_data() const {
    std::lock_guard<std::mutex> lock(mutex_);
    HASH_VALUE hash;
    hash_function->computeHash(bytes, hash);
#ifdef DEBUG_PRINTS_LEVEL_2
    printf("[PlainMessage get_hash_data][debug] Hash value: ");
    for (size_t i = 0; i < 32; ++i) {
        // 打印每个字节为两位十六进制数
        std::cout << std::hex << std::setw(2) << std::setfill('0')
                  << static_cast<int>(hash[i]);
    }
    std::cout << std::endl;
#endif
    return hash;
}

HASH_VALUE PlainMessage::get_hash_data_with_prefix(const BYTE_VECTOR& prefix) const {
    std::lock_guard<std::mutex> lock(mutex_);
    // 创建前缀+原消息的组合字节流
    BYTE_VECTOR combined;
    combined.reserve(prefix.size() + bytes.size());
    combined.insert(combined.end(), prefix.begin(), prefix.end());
    combined.insert(combined.end(), bytes.begin(), bytes.end());
    
    // 计算组合后的哈希值
    HASH_VALUE hash;
    hash_function->computeHash(combined, hash);
    
#ifdef DEBUG_PRINTS_LEVEL_2
    printf("[PlainMessage get_hash_data_with_prefix][debug] Combined hash: ");
    for (size_t i = 0; i < 32; ++i) {
        std::cout << std::hex << std::setw(2) << std::setfill('0')
                  << static_cast<int>(hash[i]);
    }
    std::cout << std::endl;
#endif
    
    return hash;
}

HASH_VALUE PlainMessage::get_hash_data_with_suffix(const BYTE_VECTOR& suffix) const {
    std::lock_guard<std::mutex> lock(mutex_);
    // 创建原消息+后缀的组合字节流
    BYTE_VECTOR combined;
    combined.reserve(bytes.size() + suffix.size());
    combined.insert(combined.end(), bytes.begin(), bytes.end());
    combined.insert(combined.end(), suffix.begin(), suffix.end());
    
    // 计算组合后的哈希值
    HASH_VALUE hash;
    hash_function->computeHash(combined, hash);
    
#ifdef DEBUG_PRINTS_LEVEL_2
    printf("[PlainMessage get_hash_data_with_suffix][debug] Combined hash: ");
    for (size_t i = 0; i < 32; ++i) {
        std::cout << std::hex << std::setw(2) << std::setfill('0')
                  << static_cast<int>(hash[i]);
    }
    std::cout << std::endl;
#endif
    
    return hash;
}

BYTE_VECTOR::const_iterator PlainMessage::get_data_begin() const
{
    return this->bytes.begin();
}
BYTE_VECTOR::const_iterator PlainMessage::get_data_end() const
{
    return this->bytes.end();

}